package io.github.loan.investor.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

import io.github.loan.common.service.BaseServiceImpl;
import io.github.loan.common.util.ConstantFinalUtil;
import io.github.loan.common.util.PageInfoUtil;
import io.github.loan.investor.dao.IBankDao;
import io.github.loan.investor.dao.IGrantDao;
import io.github.loan.investor.dao.IInvestorBankDao;
import io.github.loan.investor.dao.IInvestorDao;
import io.github.loan.investor.dao.IInvestorGrantDao;
import io.github.loan.investor.pojo.Bank;
import io.github.loan.investor.pojo.Grant;
import io.github.loan.investor.pojo.Investor;
import io.github.loan.investor.pojo.InvestorBank;
import io.github.loan.investor.pojo.InvestorGrant;
import io.github.loan.investor.service.IInvestorService;

@Service("investorService")
public class InvestorService extends BaseServiceImpl implements IInvestorService{
	@Resource
	private IGrantDao grantDao;
	@Resource
	private IInvestorDao investorDao;
	@Resource
	private IInvestorGrantDao investorGrantDao;
	@Resource
	private IInvestorBankDao investorBankDao;
	@Resource
	private IBankDao bankDao;

	@Override
	public List<Grant> findGrantCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<Grant> grantList = new ArrayList<Grant>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			grantList = this.grantDao.findCondList(condMap);
			/* grantList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(grantList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			grantList = this.grantDao.findCondList(condMap);
		}else
		{
			//不分页查询
			grantList = this.grantDao.findCondList(condMap);
		}
		return grantList;
	}

	@Override
	public Grant findOneGrantService(Map<String, Object> condMap) {
		return this.grantDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneGrantService(Grant grant) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.grantDao.insert(grant);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", grant.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneGrantService(Grant grant) {
		JSONObject resultJSON = new JSONObject();
		int res = this.grantDao.update(grant);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGrantService(Grant grant) {
		JSONObject resultJSON=new JSONObject();
		int res=this.grantDao.delete(grant);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", grant.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}

	/*出资方操作开始*/
	@Override
	public List<Investor> findInvestorCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<Investor> investorList = new ArrayList<Investor>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			investorList = this.investorDao.findCondList(condMap);
			/* investorList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(investorList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			investorList = this.investorDao.findCondList(condMap);
		}else
		{
			//不分页查询
			investorList = this.investorDao.findCondList(condMap);
		}
		return investorList;
	}

	@Override
	public Investor findOneInvestorService(Map<String, Object> condMap) {
		return this.investorDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneInvestorService(Investor investor) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.investorDao.insert(investor);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investor.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}
	/**
	 * 先更新数据库
	 * 再处理上传文件（支持回滚）
	 * 先处理新上传的文件
	 * 再删除老文件
	 * @throws FileNotFoundException 
	 */
	@Override
	public JSONObject updateOneInvestorService(Investor investor,Map<String,Object> paramMap) {
		JSONObject resultJSON = new JSONObject();
		int res = this.investorDao.update(investor);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			MultipartFile file=(MultipartFile) paramMap.get("file");
			if(file!=null&&!"".equalsIgnoreCase(file.getOriginalFilename())){
				/*原来的图片路径*/
				String oldRealPath=investor.getLogo();
				/*文件的原始名字*/
				String originalFilename=file.getOriginalFilename();
				/* 共同路径 */
				String relaPath = ConstantFinalUtil.BUNDLE.getString("upload.rootFoler") + "/investor/"
						+ this.dateFormatUtil.formatDate(new Date()) + "/" + UUID.randomUUID()  
						+ originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
				/*真实路径*/
				String truePath=paramMap.get("truePath")+"";
				/*上传文件目标路径*/
				File targetFile=new File(truePath+relaPath);
				/*目标文件创建*/
				if(!targetFile.getParentFile().exists()){
					targetFile.getParentFile().mkdirs();
				}
				try {
					if(this.fileUtil.copy(file.getInputStream(), new FileOutputStream(targetFile))){
						/*赋值图片路径*/
						investor.setLogo(relaPath);
						this.investorDao.update(investor);
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				/*删除老图片*/
				File oldFile=new File(truePath+oldRealPath);
				if(oldFile.exists()){
					if(!oldFile.delete()){
						ConstantFinalUtil.LOGGER_SYSTEM.info("删除原来图片失败了;出资方id:{},图片路径:{}",investor.getId() , 
								oldFile.getPath());
					}
				}
			}
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneInvestorService(Investor investor) {
		JSONObject resultJSON=new JSONObject();
		int res=this.investorDao.delete(investor);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", investor.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}
	/*出资方操作结束*/
	/*出资方授权操作开始*/
	@Override
	public List<InvestorGrant> findInvestorGrantCondListService(PageInfoUtil pageInfoUtil,
			Map<String, Object> condMap) {
		List<InvestorGrant> investorGrantList = new ArrayList<InvestorGrant>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			investorGrantList = this.investorGrantDao.findCondList(condMap);
			/* investorList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(investorGrantList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			investorGrantList = this.investorGrantDao.findCondList(condMap);
		}else
		{
			//不分页查询
			investorGrantList = this.investorGrantDao.findCondList(condMap);
		}
		return investorGrantList;
	}

	@Override
	public InvestorGrant findOneInvestorGrantService(Map<String, Object> condMap) {
		return this.investorGrantDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneInvestorGrantService(InvestorGrant investorGrant) {
		JSONObject resultJSON = new JSONObject();
		
		/**
		 * 添加之前，防止数据重复，先查询
		 * 根据出资方id和 授权分类id查询
		 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("investorId", investorGrant.getInvestorId());
		condMap.put("cateGrantId", investorGrant.getCateGrantId());
		InvestorGrant investorGrantRes=this.findOneInvestorGrantService(condMap);
		if(investorGrantRes!=null){
			resultJSON.put("code", "2");
			resultJSON.put("info", "已经存在;" + investorGrantRes.getId());
			return resultJSON;
		}
		int res = this.investorGrantDao.insert(investorGrant);
	
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investorGrant.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneInvestorGrantService(InvestorGrant investorGrant) {
		JSONObject resultJSON = new JSONObject();
		int res = this.investorGrantDao.update(investorGrant);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneInvestorGrantService(InvestorGrant investorGrant) {
		JSONObject resultJSON=new JSONObject();
		int res=this.investorGrantDao.delete(investorGrant);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", investorGrant.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}
	/*出资方授权操作结束*/
	@Override
	public List<InvestorBank> findInvestorBankCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<InvestorBank> investorBankList = new ArrayList<InvestorBank>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			investorBankList = this.investorBankDao.findCondList(condMap);
			/* investorList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(investorBankList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			investorBankList = this.investorBankDao.findCondList(condMap);
		}else
		{
			//不分页查询
			investorBankList = this.investorBankDao.findCondList(condMap);
		}
		return investorBankList;
	}

	@Override
	public InvestorBank findOneInvestorBankService(Map<String, Object> condMap) {
		return this.investorBankDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneInvestorBankService(InvestorBank investorBank) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.investorBankDao.insert(investorBank);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", investorBank.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneInvestorBankService(InvestorBank investorBank) {
		JSONObject resultJSON = new JSONObject();
		int res = this.investorBankDao.update(investorBank);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneInvestorBankService(InvestorBank investorBank) {
		JSONObject resultJSON=new JSONObject();
		int res=this.investorBankDao.delete(investorBank);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", investorBank.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}
	/******银行操作开始******/
	@Override
	public List<Bank> findBankCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		List<Bank> bankList = new ArrayList<Bank>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			bankList = this.bankDao.findCondList(condMap);
			/* investorList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(bankList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			bankList = this.bankDao.findCondList(condMap);
		}else
		{
			//不分页查询
			bankList = this.bankDao.findCondList(condMap);
		}
		return bankList;
	}

	@Override
	public Bank findOneBankService(Map<String, Object> condMap) {
		return this.bankDao.findOne(condMap);
	}

	@Override
	public JSONObject insertOneBankService(Bank bank) {
		JSONObject resultJSON = new JSONObject();
		
		int res = this.bankDao.insert(bank);
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			
			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", bank.getId());
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneBankService(Bank bank) {
		JSONObject resultJSON = new JSONObject();
		int res = this.bankDao.update(bank);
		
		/*String str = null ; 
		str.toString() ; */
		
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
			
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneBankService(Bank bank) {
		JSONObject resultJSON=new JSONObject();
		int res=this.bankDao.delete(bank);
		/*String str=null;
		str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
			/*放置二级JSON,影响类型*/
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("effect", res);
			dataJSON.put("id", bank.getId());
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}
}
